﻿using System;
using System.Collections;
using System.Web.UI;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;


namespace AMS.Web.BackStageManagement
{
    public partial class UserGroupUpdate : Page
    {
        readonly CastleAccess acc = new CastleAccess();
        BaseDataManager help = new BaseDataManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
            if(!IsPostBack)
            {
                labelId.Text = publ.GetUrlToReceive(Request.QueryString["id"].Trim());

                if (!string.IsNullOrEmpty(labelId.Text.Trim()))
                {
                    var objMenu = new VitCode.Castle.Entities.UserGroup();
                    IList lstObj = acc.GetObjectList(new UserGroup().GetType().Name, "Id", labelId.Text.Trim());
                    if (lstObj != null && lstObj.Count > 0)
                    {
                        objMenu = (VitCode.Castle.Entities.UserGroup)(lstObj[0]);

                        ZLTextBox_UserGroupName.Text = objMenu.Name;
                        ZLTextBox_TypeName.Text = objMenu.TypeName;
                        ZLTextBox_Memo.Text = objMenu.Memo;
                    }
                }
            }

        }

 

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserGroupList.aspx", false);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string strName = ZLTextBox_UserGroupName.Text.Trim();
            string strTypeName = ZLTextBox_TypeName.Text.Trim();
            string strMemo = ZLTextBox_Memo.Text.Trim();
            string strParentId = labelId.Text;
          
             var objUserGroup = new VitCode.Castle.Entities.UserGroup();
                IList lstObj = acc.GetObjectList(new UserGroup().GetType().Name, "Id", strParentId.Trim());
                if (lstObj != null && lstObj.Count > 0)
                {

                    IList list = help.CheckExist("from UserGroup where Name='" + strName + "' and Id != '" + labelId.Text.Trim() + "'");
                    if (list != null && list.Count > 0)
                    {
                        Alert.MessageBox("名称为: " + strName + "  的部门已存在，请检测！！！");
                        return;
                    }
                    objUserGroup = (VitCode.Castle.Entities.UserGroup)(lstObj[0]);

                }

                objUserGroup.Name = strName;
                objUserGroup.TypeName = strTypeName;
                objUserGroup.Memo = strMemo;
                 bool access =  help.SaveOrUpdate(objUserGroup);
              
                if(access) 
                    Alert.MessageBox("修改成功!");
         }
    }
}
